package com.stlm2.interceptor;


import com.stlm2.dataSource.DBContextHolder;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * 读写分离
 * Created by fl on 2015/10/14.
 */
public class DataSource implements HandlerInterceptor {



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String url = request.getRequestURI().toString();
        System.out.println(" 拦截器 访问路径："+url);
        StringBuffer requestURL = request.getRequestURL();
        String tempContextUrl = requestURL.delete(requestURL.length() - request.getRequestURI().length(), requestURL.length()).append("/").toString();
//        System.out.println(" 拦截器 访问路径："+tempContextUrl);

        String companyCode=null;
        if(request.getParameter("companyCode")!=null){
            companyCode=request.getParameter("companyCode").toString();
        }
        if(tempContextUrl.equals("http://crm.chinaddcat.com/")){
            companyCode="1";
        }else  if(tempContextUrl.equals("http://yccrm.chinaddcat.com/")){
            companyCode="2";
        }
        else  if(tempContextUrl.equals("http://xtdcrm.chinaddcat.com/")){
            companyCode="3";
        }
        else  if(tempContextUrl.equals("http://localhost/")){
//            companyCode="3";
        }
//         if (isUnContainUrl(url) && isContainUrl(url)){
//             int number = new Random().nextInt(3);
//            if(number==0){
//                DBContextHolder.setDBType(DBContextHolder.SLAVE);
//            }else if(number==1){
//                DBContextHolder.setDBType(DBContextHolder.SLAVE2);
//            }else if(number==2){
//                DBContextHolder.setDBType(DBContextHolder.SLAVE3);
//            }else{
//                DBContextHolder.setDBType(DBContextHolder.SLAVE);
//            }
//        }

        if(NumberUtils.isDigits(companyCode)){
            int nub=NumberUtils.toInt(companyCode);
            switch (nub) {
                case 1:
                    //郑州公司
                    System.out.println(" 拦截器 指定 数据源 公司编号："+001);
                    DBContextHolder.setDBType(DBContextHolder.masterData);
                    return  true;
                case 2:
                    //永城公司
                    System.out.println(" 拦截器 指定 数据源 公司编号："+002);
                    DBContextHolder.setDBType(DBContextHolder.yongChengData);
                    return  true;
                case 3:
                    //郑州新天地
                    System.out.println(" 拦截器 指定 数据源 公司编号："+003);
                    DBContextHolder.setDBType(DBContextHolder.xtdData);
                    return  true;
                default:
                    System.out.println(" 拦截器 指定 数据源 不存在公司编号："+companyCode);
                    return false;
            }
        }else {
            System.out.println(" 拦截器 指定 数据源 未传入公司编号");
            //郑州公司
            DBContextHolder.setDBType(DBContextHolder.masterData);
            return  true;

        }


    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        DBContextHolder.clearDBType();
    }


//    /**
//     * 进行读写分离的URL
//     */
//    private String[] includePatterns;
//
//    public String[] getIncludePatterns() {
//        return includePatterns;
//    }
//
//    public void setIncludePatterns(String[] includePatterns) {
//        this.includePatterns = includePatterns;
//    }
//
//    private boolean isContainUrl(String url) {
//        for (String s : this.includePatterns) {
//            if (url.contains(s)) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    /**
//     * 不进行读写分离的URL
//     */
//    private String[] excludePatterns;
//
//    public String[] getExcludePatterns() {
//        return excludePatterns;
//    }
//
//    public void setExcludePatterns(String[] excludePatterns) {
//        this.excludePatterns = excludePatterns;
//    }
//
//    private boolean isUnContainUrl(String url) {
//        for (String s : this.excludePatterns) {
//            if (url.contains(s)) {
//                return false;
//            }
//        }
//        return true;
//    }
}
